<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <title>水仙花算法 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="名词含义水仙花数（Narcissistic number），又称超完全数字不变数（pluperfect digital invariant, PPDI）、自恋数、自幂数、阿姆斯特朗数（Armstrong number）。它是指一个 n 位数（n≥3 ），它的每个数位上的数字的 n 次幂之和，等于它本身（例如：1^3 + 5^3+ 3^3 = 153）（详见互动百科）。严格来说，三位数的3次幂数才能">
<meta name="keywords" content="技术">
<meta property="og:type" content="article">
<meta property="og:title" content="水仙花算法">
<meta property="og:url" content="http://yoursite.com/2017/09/18/水仙花算法/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="名词含义水仙花数（Narcissistic number），又称超完全数字不变数（pluperfect digital invariant, PPDI）、自恋数、自幂数、阿姆斯特朗数（Armstrong number）。它是指一个 n 位数（n≥3 ），它的每个数位上的数字的 n 次幂之和，等于它本身（例如：1^3 + 5^3+ 3^3 = 153）（详见互动百科）。严格来说，三位数的3次幂数才能">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2017-10-14T01:54:07.248Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="水仙花算法">
<meta name="twitter:description" content="名词含义水仙花数（Narcissistic number），又称超完全数字不变数（pluperfect digital invariant, PPDI）、自恋数、自幂数、阿姆斯特朗数（Armstrong number）。它是指一个 n 位数（n≥3 ），它的每个数位上的数字的 n 次幂之和，等于它本身（例如：1^3 + 5^3+ 3^3 = 153）（详见互动百科）。严格来说，三位数的3次幂数才能">
  
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">个人博客</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
      </nav>
      <nav id="sub-nav">
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-水仙花算法" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2017/09/18/水仙花算法/" class="article-date">
  <time datetime="2017-09-18T13:19:09.000Z" itemprop="datePublished">2017-09-18</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/java/">java</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      水仙花算法
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="名词含义"><a href="#名词含义" class="headerlink" title="名词含义"></a>名词含义</h2><p>水仙花数（Narcissistic number），又称超完全数字不变数（pluperfect digital invariant, PPDI）、自恋数、自幂数、阿姆斯特朗数（Armstrong number）。它是指一个 n 位数（n≥3 ），它的每个数位上的数字的 n 次幂之和，等于它本身（例如：1^3 + 5^3+ 3^3 = 153）（详见互动百科）。严格来说，三位数的3次幂数才能被视为水仙花数，本例中暂不限制位数。</p>
<h2 id="问题描述"><a href="#问题描述" class="headerlink" title="问题描述"></a>问题描述</h2><p>　　求取100至100,000以内的所有水仙花数。</p>
<h2 id="思路分析："><a href="#思路分析：" class="headerlink" title="思路分析："></a>思路分析：</h2><p>1、总体框架<br>　　用for遍历指定区间，判定当前循环变量【i】是否为水仙花数：是，则输出到控制台；<br>　　问题细化为：判定某自然数【i】是否为水仙花数，具体过程由步骤2给出。<br>2、子框架：判定过程<br>　　大致分两步进行：<br>2.1、准备参数<br>　　根据【i】，算得【i】的总位数D（digit）；【i】中各位数的D次幂的和sum；<br>2.2、判定结果<br>　　比较sum和【i】值的大小，结果传给【步骤1】；<br>　　此时问题进一步细化为：计算总位数D和各位数的D次幂之和sum，具体结果分别由【步骤3】、【步骤4】给出；<br>3、D的计算<br>　　用10累除n，其商截尾取整：n/=10，再放入while语句，统计商变为0时的总累除次数，该次数+1即为D；<br>4、sum的计算<br>　　根据定义，只需利用for循环获取自然数n的各位数的值，在用sum累加各位数的D次幂即可。</p>
<h2 id="具体代码："><a href="#具体代码：" class="headerlink" title="具体代码："></a>具体代码：</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> javastudy;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NarcissisticTest</span> </span>&#123;  </span><br><span class="line">  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;  </span><br><span class="line">        getNarcissisticNumbersWithin(<span class="number">10000</span>);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">getNarcissisticNumbersWithin</span><span class="params">(<span class="keyword">int</span> target)</span> </span>&#123;  </span><br><span class="line">        System.out.printf(<span class="string">"%d(含)以内的水仙花数如下：\n"</span>,target);  </span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">100</span>; i&lt;=target; i++)&#123;  </span><br><span class="line">            <span class="keyword">if</span>(isNarcissistic(i))&#123;  </span><br><span class="line">                System.out.print(i+<span class="string">"\t"</span>);  </span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">        System.out.println(<span class="string">"\n显示完毕！"</span>);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isNarcissistic</span><span class="params">(<span class="keyword">int</span> number)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">int</span> D   = getDigit(number);  </span><br><span class="line">        <span class="keyword">int</span> sum = getSum(number, D);  </span><br><span class="line">        <span class="keyword">return</span> sum == number;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getDigit</span><span class="params">(<span class="keyword">int</span> number)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">int</span> digit = <span class="number">1</span>;  </span><br><span class="line">        <span class="keyword">while</span> ((number/=<span class="number">10</span>)&gt;<span class="number">0</span>) digit++;  </span><br><span class="line">        <span class="keyword">return</span> digit;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getSum</span><span class="params">(<span class="keyword">int</span> number, <span class="keyword">int</span> D)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">int</span> sum = <span class="number">0</span>;  </span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;D; i++)&#123;  </span><br><span class="line">            sum += Math.pow((number % <span class="number">10</span>), D);  </span><br><span class="line">            number /= <span class="number">10</span>;  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">return</span> sum;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2017/09/18/水仙花算法/" data-id="cjugipf3600001owgydovqli0" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/技术/">技术</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2019/04/14/hello-world/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Hello World
        
      </div>
    </a>
  
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    <!-- Featured Tags -->

  
    <!-- Short About -->
<section class="visible-md visible-lg">
    <h5><a href="/about/">ABOUT ME</a></h5>
    <div class="short-about">

        

        

        <!-- SNS Link -->
        <ul class="list-inline">
            
            
            

            

            

            
            
            
            
        </ul>
    </div>
</section>

  
    
  <h5>RECENT POSTS
  <div class="widget">
    <ul>
      
        <li>
          <a href="/2019/04/14/hello-world/">Hello World</a>
        </li>
      
        <li>
          <a href="/2017/09/18/水仙花算法/">水仙花算法</a>
        </li>
      
    </ul>
  </div>
</h5>
  
    <!-- Friends Blog -->

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">April 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">September 2017</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2019 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>



<script src="/js/script.js"></script>

  </div>
</body>
</html>